import java.util.Iterator;
import java.util.function.Consumer;

/**
 * 单项链表 带哨兵
 * @author 86155
 * @date 2023/10/25
 */
public class SinglyLinkedListSentinel implements Iterable<Integer>{ //链表的整体
    private Node head = new Node(666,null); //头指针 指向哨兵

    @Override
    public Iterator<Integer> iterator() {
        // 匿名内部类 -> 带名字的内部类
        return new NodeIterator();
    }

    /**
     * 节点类
     * static: 这个内部类没有引用外部类的变量时就可以加static (与外部相对独立，能加就一定要加)
     */
    private static class Node {
        int value; //值
        Node next; //下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    //头部增加
    public void addFirst(int value) {
        insert(0,value);
    }
    //遍历
    public void loop1(Consumer<Integer> consumer){
        Node pointer = head.next;
        while (pointer != null) {
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }
    //遍历
    public void loop2(Consumer<Integer> consumer){
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    //找到最后节点
    private Node findLast() {
        Node p;
        for (p = head; p.next != null; p = p.next) {

        }
        return p;
    }

    public static SinglyLinkedListSentinel getLinkedList() {
        SinglyLinkedListSentinel list = new SinglyLinkedListSentinel();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        return list;
    }
    //找到指定索引的节点
    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    //得到指定位置的值
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            //
            throw new IllegalArgumentException(
                    String.format("index out of bound[%d]",index));
        }
        return node.value;
    }

    //向尾部添加节点
    public void addLast(int v) {
        Node last = findLast();
        last.next = new Node(v,null);
    }

    private class NodeIterator implements Iterator<Integer> {
        Node p = head.next;

        @Override
        public boolean hasNext() { //是否有下一个元素
            return p!=null;
        }

        @Override
        public Integer next() { //返回当前值，并指向下一个元素
            int value = p.value;
            p = p.next;
            return value;
        }
    }

    //像索引位置插入
    public void insert(int index, int value) {
        Node foreNode = findNode(index - 1);//上一个节点
        if (foreNode == null) {
            throw new IllegalArgumentException("index 异常");
        }
        foreNode.next = new Node(value,foreNode.next);

    }

    //删除第一个节点
    public void removeFirst() {
        remove(0);
    }

    public void remove(int index) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException("null");
        }
        Node removed = prev.next; //被删除的节点
        if (removed == null) {
            throw new IllegalArgumentException("");
        }
        prev.next = removed.next;
    }
}

